package com.elective.courseselect.serviceUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elective.courseselect.entity.CourseQuestionnaireEntity;
import com.elective.courseselect.entity.QuestionEntity;
import com.elective.courseselect.entity.QuestionnaireEntity;
import com.elective.courseselect.service.CourseQuestionnaireService;
import com.elective.courseselect.service.CourseService;
import com.elective.courseselect.service.QuestionService;
import com.elective.courseselect.service.QuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author lee
 * @date 2021年09月19日 21:55
 */
@Component
public class QuestionUtils {

    public static final String RADIO = "radio";
    public static final String MULTI_SELECT = "multi-select";
    public static final String JUDGE = "judge";
    public static final String OBJECT = "object";

    @Autowired
    QuestionService questionService;

    @Autowired
    QuestionnaireService questionnaireService;

    @Autowired
    CourseQuestionnaireService courseQuestionnaireService;

    @Autowired
    CourseService courseService;

    public static QuestionUtils questionUtils;



    @PostConstruct
    public void init(){
        questionUtils=this;   //必需
        questionUtils.questionService=this.questionService;
        questionUtils.questionnaireService=this.questionnaireService;
        questionUtils.courseQuestionnaireService=this.courseQuestionnaireService;
        questionUtils.courseService=this.courseService;
        questionUtils.questionUtils=this.questionUtils;
    }

    /**
     * 根据QuestionnaireId查询一套卷子所有题目
     * @author Lee
     * @date 2021/9/20 0:29
     * @param questionnaireId
     * @return java.util.List<com.elective.courseselect.entity.QuestionEntity>
     */
    public static List<QuestionEntity> searchQuestionListByQuestionnaireId(Integer questionnaireId) {
        QueryWrapper<QuestionEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id", questionnaireId);
        List<QuestionEntity> list = questionUtils.questionService.list(wrapper);
        return list;
    }

    public static Integer baseOfQuestionnaireIdGetCourseId(Integer questionnaireId) {
        QuestionnaireEntity questionnaireServiceById = questionUtils.questionnaireService.getById(questionnaireId);
        QueryWrapper<CourseQuestionnaireEntity> courseQuestionnaireEntityQueryWrapper = new QueryWrapper<>();
        courseQuestionnaireEntityQueryWrapper.eq("questionnaire_id", questionnaireId);
        CourseQuestionnaireEntity courseQuestionnaireServiceOne = questionUtils.courseQuestionnaireService.getOne(courseQuestionnaireEntityQueryWrapper);
        if (null == questionnaireServiceById || null == courseQuestionnaireServiceOne) {
            return null;
        }else{
            Integer courseId = courseQuestionnaireServiceOne.getCourseId();
            return courseId;
        }
    }

}
